N values are updated, I want to know the minimum value.
There are N values and they are being updated, I want to know the minimum value.
→ Queue a tuple of which value was updated to what and when. Store the last modified time of the value in an array of size N. Skip information that is not up-to-date when retrieving.
code:python
N = 2
queue = []
time = 0
def update(pos, value):
global time
time += 1
heappush(queue, (value, pos, time))
def top():
while queue:
value, pos, time = queue0 if time == lastUpdatepos: return value
heappop(queue)
return None
update(0, 42)
print(top()) # => 42
update(1, 43)
print(top()) # => 42
update(0, 44)
print(top()) # => 43
---
This page is auto-translated from /nishio/N個の値が更新される、最小値を知りたい. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.